<?php

/**
 * 发货设置
 */

namespace app\seller\controller;

class DeliverSet extends \app\common\controller\Seller
{

    /**
     * 发货地址列表
     */
    public function daddress()
    {
        $model_daddress = model('Common/Daddress');
        $condition = array();
        $condition['store_id'] = $this->store_id;
        $address_list = $model_daddress->getAddressList($condition, '*', '', 20);
        $this->assign('address_list', $address_list);
        $this->profile_menu('daddress', 'daddress');
        return $this->fetch('deliver/daddress_list');
    }

    /**
     * 新增/编辑发货地址
     */
    public function daddress_add()
    {
        $model_daddress = model('Common/Daddress');
        if ($this->request->isPost()) {
            //保存 新增/编辑 表单
            $obj_validate = new \think\Validate([
                'seller_name|发货人' => 'require',
                'area_id|所在地区' => 'require|number',
                'city_id|所在地区' => 'require|number',
                'area_info|所在地区信息' => 'require',
                'address|详细地址' => 'require',
                'telphone|手机号码' => 'require',
            ]);
            
            $data = input('post.');
            if (!$obj_validate->check($data)) {
                return showDialog($obj_validate->getError(), '', 'error');
            }
            
            $address_id = intval($data['address_id']);
            $data = array(
                'store_id' => $this->store_id,
                'seller_name' => $data['seller_name'],
                'area_id' => $data['area_id'],
                'city_id' => $data['city_id'],
                'area_info' => $data['area_info'],
                'address' => $data['address'],
                'telphone' => $data['telphone'],
                'company' => $data['company']
            );
            if ($address_id > 0) {
                $condition = array();
                $condition['address_id'] = $address_id;
                $condition['store_id'] = $this->store_id;
                $model_daddress->editAddress($data, $condition);
            } else {
                $insert = $model_daddress->addAddress($data);
                if (!$insert) {
                    return showDialog('添加地址失败', '', 'error');
                }
            }
            return showDialog('操作成功', 'reload', 'succ', 'CUR_DIALOG.close()');
        } elseif (is_numeric($_GET['address_id']) > 0) {
            //编辑
            $condition = array();
            $condition['address_id'] = intval($_GET['address_id']);
            $condition['store_id'] = $this->store_id;
            $address_info = $model_daddress->getAddressInfo($condition);
            if (empty($address_info) && !is_array($address_info)) {
                return showMessage('参数不正确', '/?s=seller/deliver_set/daddress', 'html', 'error');
            }
            $this->assign('address_info', $address_info);
        }
        return $this->fetch('deliver/daddress_add');
    }

    /**
     * 删除发货地址
     */
    public function daddress_del()
    {
        $address_id = intval($_GET['address_id']);
        if ($address_id <= 0) {
            return showDialog('删除失败', '', 'error');
        }
        $condition = array();
        $condition['address_id'] = $address_id;
        $condition['store_id'] = $this->store_id;
        $delete = Model('daddress')->delAddress($condition);
        if ($delete) {
            return showDialog('删除成功', url('seller/deliver_set/daddress'), 'succ');
        } else {
            return showDialog('删除失败', '', 'error');
        }
    }

    /**
     * 设置默认发货地址
     */
    public function daddress_default_set()
    {
        $address_id = intval($_GET['address_id']);
        if ($address_id <= 0) {
            return false;
        }
        $condition = array();
        $condition['store_id'] = $this->store_id;
        model('Common/Daddress')->editAddress(array('is_default' => 0), $condition);
        $condition['address_id'] = $address_id;
        model('Common/Daddress')->editAddress(array('is_default' => 1), $condition);
    }

    public function express()
    {
        $model = model('Common/StoreExtend');

        if ($this->request->isPost()) {
            $data['store_id'] = $this->store_id;
            $cexpress = input('post.cexpress/a');
            if ($cexpress) {
                $data['express'] = implode(',', $cexpress);
            } else {
                $data['express'] = '';
            }
            
            if (!$model->getStoreExtendInfo($this->store_id)) {
                $result = $model->insert($data);
            } else {
                $result = $model->update($data);
            }
            
            if ($result) {
                return showDialog('保存成功', 'reload', 'succ');
            } else {
                return showDialog('保存失败', 'reload', 'error');
            }
        }

        $express_list = \Express::lists();

        //取得店铺启用的快递公司ID
        $express_select = $model->getStoreExtendInfo($this->store_id, 'express');
        if (!is_null($express_select)) {
            $express_select = explode(',', $express_select);
        } else {
            $express_select = array();
        }
        $this->assign('express_select', $express_select);
        //页面输出
        $this->profile_menu('daddress', 'express');
        $this->assign('express_list', $express_list);
        return $this->fetch('deliver/express');
    }

    /**
     * 免运费额度设置
     */
    public function free_freight()
    {
        $model_store = model('Common/Store');
        if ($this->request->isPost()) {
            $store_free_price = floatval(abs(input('store_free_price')));
            $model_store->editStore(array('store_free_price' => $store_free_price), array('id' => $this->store_id));
            return showDialog('保存成功', '', 'succ');
        }
        
        $store_info = $model_store->getStoreInfo($this->store_id, false);
        $this->assign('store_free_price', $store_info['store_free_price']);
        $this->profile_menu('daddress', 'free_freight');
        return $this->fetch('deliver/free_freight');
    }

    /**
     * 发货单打印设置
     */
    public function print_set()
    {
        $model_store = model('Common/Store');
        $store_info = $model_store->getStoreInfo($this->store_id, false);
        if ($this->request->isPost()) {
            $obj_validate = new \think\Validate([
                'store_printdesc|备注信息' => 'require|length:1,200'
            ]);
            
            $data = input('post.');
            if (!$obj_validate->check($data)) {
                return showDialog($obj_validate->getError());
            }
            
            $update_arr = array();
            //上传认证文件
            if ($_FILES['store_stamp']['name'] != '') {
                $upload = $this->uploadFile('store_stamp', 'stamp', false, ['ext' => 'gif,png,jpg']);
                if ($upload['status']) {
                    $update_arr['store_stamp'] = $upload['save_path_type'].$upload['save_name'];
                } else {
                    return showDialog('图片上传失败：'.$upload['error'], '', 'error');
                }
                
                if (!empty($store_info['store_stamp'])) {
                    $file = ROOT_PATH.'/public/uploads/'.$store_info['store_stamp'];
                    is_file($file) && @unlink($file);
                }
            }
            $update_arr['store_printdesc'] = $data['store_printdesc'];
            $rs = $model_store->editStore($update_arr, ['id' => $this->store_id]);
            if ($rs) {
                return showDialog('保存成功', 'reload', 'succ');
            } else {
                return showDialog('保存失败', '', 'error');
            }
        } else {
            $this->assign('store_info', $store_info);
            $this->profile_menu('daddress', 'print_set');
            return $this->fetch('deliver/print_set');
        }
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string	$menu_type	导航类型
     * @param string 	$menu_key	当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_type, $menu_key = '')
    {
        switch ($menu_type) {
            case 'daddress':
                $menu_array = array(
                    array('menu_key' => 'daddress', 'menu_name' => '地址库', 'menu_url' => url('seller/deliver_set/daddress')),
                    array('menu_key' => 'express', 'menu_name' => '默认物流公司', 'menu_url' => url('seller/deliver_set/express')),
                    array('menu_key' => 'free_freight', 'menu_name' => '免运费额度', 'menu_url' => url('seller/deliver_set/free_freight')),
                    array('menu_key' => 'print_set', 'menu_name' => '发货单打印设置', 'menu_url' => url('seller/deliver_set/print_set'))
                );
                break;
        }
        $this->assign('member_menu', $menu_array);
        $this->assign('menu_key', $menu_key);
        $this->assign('left_nav', 'daddress');
    }

}
